package com.earnings.okhttputils.utils.ui.activity.user;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.UrlConstant.Constant;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.bean.UserBankData;
import com.earnings.okhttputils.utils.god.GodAdapter.GodBaseAdapter;
import com.earnings.okhttputils.utils.god.GodAdapter.GodViewHolder;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.https.HttpArrayCallback;
import com.earnings.okhttputils.utils.https.HttpMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.earnings.R.id.account;


/*
编辑用户信息-银行卡
 */

public class UserEditBankActivity extends GodLeftHandBaseActivity implements View.OnClickListener {

    private RecyclerView rvMsg;
    public static  final  int TYPE_RETURN=1;
    private TextView info;
    private ArrayList<UserBankData> datas;
    private GodBaseAdapter<UserBankData> buttonAdapter;

    @Override
    protected int onCreateLayoutId() {
        return  R.layout.activity_user_edit_bank;
    }

    @Override
    protected void init() {

        setTopStyleView();
        setTitle("银行卡");
        showRightImage( R.mipmap.add_white_icon );
        findViewById(R.id.top_img).setOnClickListener(this);
        rvMsg= (RecyclerView) findViewById(R.id.recyclerview);
        info= (TextView) findViewById(R.id.info);

    }




public void initBanksItem(){
    rvMsg.setLayoutManager(new LinearLayoutManager(this));
    if(bundleData.containsKey( "type" )){
    buttonAdapter=new GodBaseAdapter<UserBankData>(R.layout.item_bank_withdrawals,datas) {
        @Override
        public void onBind(GodViewHolder holder, int position, UserBankData data) {
            if(!bundleData.containsKey("id"))return;
            if(bundleData.getString( "id" ).equals( data.getId() ))
                holder.setVisibility( R.id.icon,View.VISIBLE );
            ImageView logoImageview= (ImageView) holder.getView( R.id.logo );
            holder.setText( R.id.name,data.getAccount_tname()+"("+data.getAccount().substring( data.getAccount().length()-4,data.getAccount().length() )+")" );
            holder.setText( R.id.info,data.getAccount_tsub() );
           // holder.setText( R.id.account,data.getAccount() );
            Glide.with(getContext()).load(data.getBank_logo() ).error( R.mipmap.head_icon ).into(logoImageview);


        }

        @Override
        public void OnItemClickListener(View view, int position, UserBankData data) {
            super.OnItemClickListener( view, position, data );
               bundleData.putString( "id",data.getId() );
                bundleData.putString( "name" ,data.getAccount_tname());
                bundleData.putString( "account" ,data.getAccount());
                bundleData.putString( "logo" ,data.getBank_logo());
                mIntent.putExtras( bundleData );
                setResult( TYPE_RETURN ,mIntent);
                finish();

        }
    };
    }else{
        buttonAdapter=new GodBaseAdapter<UserBankData>(R.layout.item_bank,datas) {
            @Override
            public void onBind(GodViewHolder holder, int position, UserBankData data) {
                ImageView logoImageview= (ImageView) holder.getView( R.id.logo );
                holder.setText( R.id.name,data.getAccount_tname() );
                holder.setText( R.id.info,data.getAccount_tsub() );
                holder.setText( account,data.getAccount() );
                Glide.with(getContext()).load(data.getBank_logo() ).error( R.mipmap.head_icon ).into(logoImageview);


            }

            @Override
            public void OnItemClickListener(View view, int position, UserBankData data) {
                super.OnItemClickListener( view, position, data );
                bundleData.putString( "id",data.getId() );


                    skipActivity( UserEditBankInfoActivity.class );

            }
        };
    }

    rvMsg.setAdapter(buttonAdapter);
}


    @Override
    public void onClick(View v) {

        int i = v.getId();
        if (i == R.id.top_img) {
            skipActivity( UserEditBankAddActivity.class );

        }
    }

    public void loadBanksData() {
        if(datas!=null){
            datas.clear();}
        Map map = new HttpMap(getContext());


        OkHttpUtils.post().url( HttpUrl.USER_BANK_LOAD_URL ).params( map ).build().execute( new HttpArrayCallback<UserBankData>(  getContext()) {
            @Override
            public void onSuccess(List<UserBankData> object, String msg) {

                datas= (ArrayList<UserBankData>) object;
                initBanksItem();
                info.setVisibility( View.GONE );

                if(buttonAdapter!=null){
                    buttonAdapter.notifyDataSetChanged();}
            }
            @Override
            public void onFail(int code, String msg) {
                info.setText( msg );
                info.setVisibility( View.VISIBLE );
                //ToastUtils.showToast( getContext(),msg );
            }


        } );

    }

    @Override
    protected void onResume() {
        super.onResume();
        loadBanksData();
    }

    @Override
    public void finish() {
        super.finish();
        setResult( Constant.USER_UPDATE );
    }
}
